package net.lzzy.practices.web.controller;

import io.swagger.annotations.Api;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Chapter;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.services.ChapterService;
import net.lzzy.practices.services.CourseService;
import net.lzzy.practices.utils.StringUtil;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.ChapterInfo;
import net.lzzy.practices.web.models.VmChapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author m
 */
@RestController
@CrossOrigin
@RequestMapping(ApiConstant.ROUTE_CHAPTER_ROOT)
@Api(tags = "章节相关业务")
public class ChapterApiController {
    private final ChapterService service;
    private final CourseService cService;

    @Autowired
    public ChapterApiController(ChapterService service, CourseService cService) {
        this.service = service;
        this.cService = cService;
    }
    @GetMapping(ApiConstant.ROUTE_CHAPTER_TEACHER_CHAPTERS )
    public ApiResult getTeachersChapters(@RequestParam String token, @RequestParam UUID courseId) {
        List<VmChapter> chapters = new ArrayList<>();
        service.getTeachersChapters(courseId, token).forEach(chapter -> chapters.add(VmChapter.create(chapter)));
        return new ApiResult(true, chapters.size() + "", chapters);
    }
    @GetMapping(ApiConstant.ROUTE_CHAPTER_STUDENT_CHAPTERS )
    public ApiResult getStudentsChapters(@RequestParam String token, @RequestParam UUID courseId) {
        List<VmChapter> chapters = new ArrayList<>();
        service.getStudentsChapters(courseId,token).forEach(chapter -> chapters.add(VmChapter.create(chapter)));
        return new ApiResult(true, chapters.size() + "", chapters);
    }
    private ApiResult addOrUpdate(ChapterInfo info, UUID id) {
        String token = info.getToken();
        String name = info.getName();
        String description = info.getDescription();
        boolean open = info.isOpen();
        String courseId = info.getCId();
        if (StringUtil.isEmpty(name) ||
                StringUtil.isEmpty(description) ||
                StringUtil.isEmpty(courseId)) {
            return new ApiResult(false, Messages.MISS_ARGS.toString(), null);
        }
        Course course =cService.getById(UUID.fromString(courseId));
        if (course == null) {
            return new ApiResult(false, Messages.WRONG_ID.toString(), null);
        }
        Chapter chapter;
        boolean add = id == null;
        if (add) {
            chapter = new Chapter();
            chapter.setCourse(course);
        } else {
            chapter = service.getById(id);
            if (chapter == null) {
                return new ApiResult(false, Messages.WRONG_ID.toString(), null);
            }
        }
        chapter.setUpdateTime(new Date());
        chapter.setOpen(open);
        chapter.setName(name);
        chapter.setDescription(description);
        if (add) {
            return new ApiResult(true, Messages.SUCCESC.toString(), VmChapter.create(service.add(chapter)));
        } else {
            Chapter updated = service.update(chapter, token);
            if (updated == null) {
                return new ApiResult(false, Messages.NO_PERISSION.toString(), null);
            }
            return new ApiResult(true, Messages.SUCCESC.toString(), VmChapter.create(updated));
        }
    }
    @PostMapping(ApiConstant.ROUTE_CHAPTER_ADD)
    public ApiResult add(@RequestBody ChapterInfo info) {
        return addOrUpdate(info, null);
    }

    @PostMapping(ApiConstant.ROUTE_CHAPTER_UPDATE)
    public ApiResult update(@RequestBody ChapterInfo info,@RequestParam UUID id){
        return addOrUpdate(info,id);
    }
    @PostMapping(ApiConstant.ROUTE_CHAPTER_OPEN_CLOSE)
    public ApiResult openClose(@RequestBody ChapterInfo info){
        String token=info.getToken();
        String strCourseId=info.getCId();
        boolean open=info.isOpen();
        if(StringUtil.isEmpty(token)||StringUtil.isEmpty(strCourseId)){
            return new ApiResult(false,Messages.MISS_ARGS.toString(), null);
        }
        Chapter chapter=service.openCloseChapter(UUID.fromString(strCourseId),open,token);
        if (chapter==null){
            return new ApiResult(false,Messages.NO_PERISSION.toString(), null);
        }
        return new ApiResult(true,Messages.SUCCESC.toString(), VmChapter.create(chapter));
    }
    @DeleteMapping(ApiConstant.ROUTE_CHAPTER_REMOVE)
    public ApiResult delete(@RequestBody ChapterInfo info){
        String token=info.getToken();
        String strCourseId=info.getCId();
        String strChapter=info.getId();
        if(StringUtil.isEmpty(token)||StringUtil.isEmpty(strCourseId)||StringUtil.isEmpty(strChapter)){
            return new ApiResult(false,Messages.MISS_ARGS.toString(), null);
        }
        try {
            UUID courseId=UUID.fromString(strCourseId);
            UUID chapterId=UUID.fromString(strChapter);
            Messages msg=service.remove(courseId,chapterId,token);
            return new ApiResult(msg==Messages.SUCCESC,msg.toString(),null);
        }catch (Exception e){
            return new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }

    }
}